package com.allwees.bs.c.module.order.entity;

import com.allwees.bs.c.module.base.entity.JpaBaseEntity;
import com.allwees.bs.c.module.order.req.CartItemReq;
import com.allwees.bs.c.module.product.dto.SkuDTO;
import com.allwees.bs.c.module.product.entity.ProductEntity;
import com.allwees.bs.c.module.product.entity.SkuEntity;
import com.allwees.bs.c.module.product.service.ProductService;
import com.allwees.core.common.util.BeanUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.math.BigDecimal;

/**
 *
 *
 * @author yangylsky
 *
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity()
@Table(name = "o_cart_item")
public class CartItemEntity extends JpaBaseEntity {

	// CartID
	@Column(name = "cart_uuid")
	private String cartUuid;

    //商品ID
    @Column(name = "product_uuid")
    private String productUuid;

	// 商品SKU ID
	@Column(name = "sku_uuid")
	private String skuUuid;

	// 购买数量
	private Integer quantity;

	@Transient
	private ProductEntity product;

	@Transient
	private SkuEntity sku;

	public CartItemEntity(CartItemReq itemReq) {
		this();
		this.skuUuid = itemReq.getSkuUuid();
		this.quantity = itemReq.getQuantity();
		SkuDTO sku = BeanUtil.getBean(ProductService.class).findSkuByUuid(itemReq.getSkuUuid());
		this.setProductUuid(sku.getProductUuid());
	}

	public BigDecimal amount() {
		return sku.getRetailPrice().multiply(new BigDecimal(getQuantity()));
	}
}
